#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
//设置风速显示
void Set_Display_FanState(uint8_t FanSate);

/* widgets:
Layer_KT_FanSet
Background_KTFanSet
PopupButton_FanSetBack
Container_FanSet_On
PopupButton_FanHigh_On
PopupButton_FanMiddle_On
PopupButton_FanLow_On
PopupButton_FanAuto_On
Container_FanSet_Off
Animation_FanHigh_Off
PopupButton_FanHigh_Off
PopupButton33
Animation_FanMiddle_Off
PopupButton_FanMiddle_Off
PopupButton37
PopupButton38
Animation_FanLow_Off
PopupButton_FanLow_Off
PopupButton43
PopupButton44
PopupButton45
Animation_FanAuto_Off
PopupButton_FanAuto_Off
PopupButton51
PopupButton52
PopupButton53
PopupButton54
*/
static ITUPopupButton* PopupButton_FanAuto_On;
static ITUPopupButton* PopupButton_FanLow_On;
static ITUPopupButton* PopupButton_FanMiddle_On;
static ITUPopupButton* PopupButton_FanHigh_On;
static uint8_t Fan_Temp;
static uint8_t DwellTime = 0;
bool KTFanSetOnEnter(ITUWidget* widget, char* param)
{
	if (!PopupButton_FanAuto_On)
	{
		PopupButton_FanAuto_On = ituSceneFindWidget(&theScene, "PopupButton_FanAuto_On");
		assert(PopupButton_FanAuto_On);
		PopupButton_FanLow_On = ituSceneFindWidget(&theScene, "PopupButton_FanLow_On");
		assert(PopupButton_FanLow_On);
		PopupButton_FanMiddle_On = ituSceneFindWidget(&theScene, "PopupButton_FanMiddle_On");
		assert(PopupButton_FanMiddle_On);
		PopupButton_FanHigh_On = ituSceneFindWidget(&theScene, "PopupButton_FanHigh_On");
		assert(PopupButton_FanHigh_On);
	}
	//根据状态显示风速
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Speed)
		Set_Display_FanState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Speed);
	else
		Set_Display_FanState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Speed);
	Set_Rotate_Block(360);//旋转步进
    return true;
}

bool KTFanSetOnLeave(ITUWidget* widget, char* param)
{
	if (Fan_Temp != TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Speed)//状态有改变
	{
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Speed = Fan_Temp;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Speed = Fan_Temp;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.status = 0x57;//设置风速
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Speed = Fan_Temp;
		printf("Moorgen INFO : Touch to set the air conditioner's fan speed.\n");
		RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	}
	DwellTime = 0;
	MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
    return true;
}
extern bool KTFanSetOnTimer(ITUWidget* widget, char* param)
{
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
		Clear_User_Action();
	//外部事件状态更新
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.status == 1)//设备开处理旋转动作
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation)//顺时针旋转
		{
			DwellTime = 0;
			if (Fan_Temp < 3)
			{
				Fan_Temp++;
				Set_Display_FanState(Fan_Temp);
			}
			Clear_User_Action();
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation)//逆时针旋转
		{
			DwellTime = 0;
			if (Fan_Temp > 0)
			{
				Fan_Temp--;
				Set_Display_FanState(Fan_Temp);
			}
			Clear_User_Action();
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		}
	}
	if (DwellTime < 250)
		DwellTime++;
	else
	{
		ITULayer *Layer;
		DwellTime = 0;
		Layer = ituSceneFindWidget(&theScene, "Layer_KongTiao");
		assert(Layer);
		ituLayerGoto(Layer);
	}
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		//根据状态显示风速
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Speed)
			Set_Display_FanState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Extend_Speed);
		else
			Set_Display_FanState(TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Air_Status.Air_Status_Value.Byte.Speed);
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	return true;
}

bool PopupButton_KT_FanSetHigh_Press(ITUWidget* widget, char* param)
{
	Fan_Temp = 3;
	Set_Display_FanState(Fan_Temp);
    return true;
}

bool PopupButton_KT_FanSetMiddle_Press(ITUWidget* widget, char* param)
{
	Fan_Temp = 2;
	Set_Display_FanState(Fan_Temp);
    return true;
}

bool PopupButton_KT_FanSetLow_Press(ITUWidget* widget, char* param)
{
	Fan_Temp = 1;
	Set_Display_FanState(Fan_Temp);
    return true;
}

bool PopupButton_KT_FanSetAuto_Press(ITUWidget* widget, char* param)
{
	Fan_Temp = 0;
	Set_Display_FanState(Fan_Temp);
    return true;
}
bool PopupButton_KT_FanSetExit_Press(ITUWidget* widget, char* param)
{
	return true;
}
//显示风速
void Set_Display_FanState(uint8_t FanSate)
{
	Fan_Temp = FanSate;
	if (Fan_Temp > 3)return;
	//根据状态显示风速
	ituWidgetSetVisible(PopupButton_FanAuto_On, false);
	ituWidgetSetVisible(PopupButton_FanLow_On, false);
	ituWidgetSetVisible(PopupButton_FanMiddle_On, false);
	ituWidgetSetVisible(PopupButton_FanHigh_On, false);
	if (Fan_Temp == 0)
	{
		//自动
		ituWidgetSetVisible(PopupButton_FanAuto_On, true);
	}
	else if (Fan_Temp == 1)
	{
		//低风
		ituWidgetSetVisible(PopupButton_FanLow_On, true);
	}
	else if (Fan_Temp == 2)
	{
		//中风
		ituWidgetSetVisible(PopupButton_FanMiddle_On, true);
	}
	else if (Fan_Temp == 3)
	{
		//高风
		ituWidgetSetVisible(PopupButton_FanHigh_On, true);
	}
}